package guowenlong.golf.ui.dynamic;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;
import guowenlong.golf.C;
import guowenlong.golf.R;
import guowenlong.golf.base.BaseFragment;
import guowenlong.golf.ui.dynamic.message.MessageFragment;
import guowenlong.golf.ui.dynamic.news.NewsFragment;
import guowenlong.golf.ui.dynamic.notify.NotifyActivity;

/**
 * des   : 动态 author: Administrator email : guowenlong20000@gmail.com time  : 2017年02月10日 下午 3:19.
 */

public class DynamicFragment extends BaseFragment<DynamicPresenter, DynamicModel> implements
    DynamicContract.View {

  @BindView(R.id.rb_news)
  RadioButton rbNews;
  @BindView(R.id.rb_message)
  RadioButton rbMessage;
  @BindView(R.id.btn_notify)
  Button btnNotify;
  NewsFragment mNewsFragment;
  MessageFragment mMessageFragment;
  @BindView(R.id.fl_fragment_dynamic)
  FrameLayout flFragmentDynamic;
  Unbinder unbinder;

  @Override
  public void initData(Bundle savedInstanceState) {

  }

  @Override
  public void initView() {
    rbNews.setChecked(true);
    selectFragment(C.FRAGMENT_DYNAMIC_NEWS);
  }

  @Override
  public int getLayoutId() {
    return R.layout.fragment_dynamic;
  }

  @OnClick({R.id.rb_news, R.id.rb_message,R.id.btn_notify})
  public void onClick(View view) {
    switch (view.getId()) {
      case R.id.rb_news:
        selectFragment(C.FRAGMENT_DYNAMIC_NEWS);
        break;
      case R.id.rb_message:
        selectFragment(C.FRAGMENT_DYNAMIC_MESSAGE);

        break;
      case R.id.btn_notify:
        startActivity(new Intent(getActivity(), NotifyActivity.class));
        break;
    }
  }

  public void selectFragment(int fragmentNumber) {
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    hideFragments(transaction);
    switch (fragmentNumber) {
      case C.FRAGMENT_DYNAMIC_NEWS:
        rbNews.setChecked(true);
        if (mNewsFragment == null) {
          mNewsFragment = new NewsFragment();
          transaction.add(R.id.fl_fragment_dynamic, mNewsFragment);
        }
        transaction.show(mNewsFragment);
        break;
      case C.FRAGMENT_DYNAMIC_MESSAGE:
        rbMessage.setChecked(true);
        if (mMessageFragment == null) {
          mMessageFragment = new MessageFragment();
          transaction.add(R.id.fl_fragment_dynamic, mMessageFragment);
        }
        transaction.show(mMessageFragment);
        break;
    }
    transaction.commit();
  }

  public FragmentTransaction hideFragments(FragmentTransaction transaction) {
    if (mNewsFragment != null) {
      transaction.hide(mNewsFragment);
    }
    if (mMessageFragment != null) {
      transaction.hide(mMessageFragment);
    }
    return transaction;
  }
}
